package com.finalproject.ui;

/**
 * CafeItem for UI
 */

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.finalproject.entity.CafeItem;

public class UICafeItem extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel nameLabel;
	private JLabel priceLabel;
	private JLabel caloriesLabel;
	private JButton buyButton;
	private JButton infoButton;
	private JLabel preparationTimeLabel;
	private DecimalFormat priceFormat = new DecimalFormat("$0.00");
	
	private CafeItem cafeItem;
	/**
	 * Create the panel.
	 */
	public UICafeItem(CafeItem cafeItem) {
		this.cafeItem = cafeItem;
		String name = cafeItem.getName();
		Integer calories = cafeItem.getCalories();
		String price = priceFormat.format(cafeItem.getPrice());
		int prepTime = cafeItem.getPrepTime();
		//setLayout(new FlowLayout());
		BorderLayout borderLayout = new BorderLayout();
		setLayout(borderLayout);
		borderLayout.setHgap(3);
		borderLayout.setVgap(3);
		
		
		setBorder(BorderFactory.createTitledBorder("Menu"));
		JPanel labelPanel = new JPanel();
		BorderLayout labelBorderLayout = new BorderLayout();
		labelBorderLayout.setVgap(3);
		labelBorderLayout.setHgap(3);
		labelPanel.setLayout(labelBorderLayout);
		
		nameLabel = new JLabel(name,SwingConstants.LEFT);
		labelPanel.add(nameLabel,BorderLayout.NORTH);
		String calStr = "" + calories.toString()  +" cal";
		caloriesLabel = new JLabel(calStr,SwingConstants.LEFT);
		labelPanel.add(caloriesLabel,BorderLayout.CENTER);
		
		preparationTimeLabel = new JLabel( "[" + prepTime +" minutes prep time]", SwingConstants.LEFT);
		
		priceLabel = new JLabel("(" + price.toString() +  ")",SwingConstants.LEFT);
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BorderLayout());
		centerPanel.add(priceLabel,BorderLayout.NORTH);
		centerPanel.add(preparationTimeLabel,BorderLayout.CENTER);
		labelPanel.add(centerPanel,BorderLayout.SOUTH);
		add(labelPanel, BorderLayout.CENTER);
		nameLabel.setHorizontalTextPosition(JLabel.CENTER);

		
		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new BorderLayout());
		infoButton = new JButton("Info");
		
		infoButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				showinfo();
			}
			
		});
		rightPanel.add(infoButton, BorderLayout.SOUTH);

		buyButton = new JButton("Order");
		//buyButton.setEnabled(false);
		rightPanel.add(buyButton, BorderLayout.NORTH);
		
		add(rightPanel,BorderLayout.EAST);
		
	//	setBorder(BorderFactory.createLineBorder(Color.black));
		setPreferredSize(new Dimension(550,100));
		setSize(550,150);
		setVisible(true);
		
	}
	public UICafeItem(CafeItem cafeItem2, int i, double d) {
		// TODO Auto-generated constructor stub
	}
	private void showinfo() {
		
		String name = "Name:" + cafeItem.getName() + "\n";
	//	String description = "Description:" + cafeItem.getDescription() + "\n";
		String description = "";
		String calories = "Calories: " + cafeItem.getCalories().toString() + "cal\n";
		String price = "Price: " + priceFormat.format(cafeItem.getPrice()) + "\n";
		String fat = "Fat :" + cafeItem.getFat() + "g\n";
		String cholestrol = "Cholestrol: " + cafeItem.getCholestrol().toString() + "mg\n";
		String carbs = "Carbohydrate: " + cafeItem.getCarbs().toString() + "g\n";
		String sodium = "Sodium: " + cafeItem.getSodium().toString() + "mg\n";
		String protein = "Protein: " + cafeItem.getProtein().toString() + "g";
		String message = name + description + calories + price + fat + cholestrol + carbs + sodium + protein ;
		JOptionPane.showConfirmDialog(this, message, "Snack Item information",JOptionPane.PLAIN_MESSAGE);

	}
	
	public JLabel getNameLabel() {
		return nameLabel;
	}
	
	public JButton getBuyButton() {
		return buyButton;
	}
	
	
	public JButton getInfoButton() {
		return infoButton;
	}
	
}
